package com.yanq.liancar.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.RxBarTool;
import com.vondear.rxtool.view.RxToast;
import com.yanq.liancar.R;
import com.yanq.liancar.activity.base.BaseToolbarActivity;
import com.yanq.liancar.adapter.SmallBPriceDetailedAdapter;
import com.yanq.liancar.bean.QueryBalanceByUserBean;
import com.yanq.liancar.bean.UserBean;
import com.yanq.liancar.url.Config;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SmallBSettlementActivity extends BaseToolbarActivity {
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.price_recyclerview)
    RecyclerView priceRecyclerview;
    @BindView(R.id.smartrefreshlayout)
    SmartRefreshLayout smartrefreshlayout;

    private int page = 1;
    private boolean isClear = true;
    private List<QueryBalanceByUserBean.DataBean.RecordBean> goodsListBeans=new ArrayList<>();
    private SmallBPriceDetailedAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_small_b_settlement);
        ButterKnife.bind(this);
        RxBarTool.setStatusBarColor(this, R.color.main_color);

        refreshListener();
        initRecyclerView();

        getJiesuan();
    }

    @OnClick(R.id.img_back)
    public void onViewClicked() {
        finish();
    }


    private void initRecyclerView() {
        priceRecyclerview.setNestedScrollingEnabled(false);
        priceRecyclerview.setLayoutManager(new LinearLayoutManager(mContext));
        adapter=new SmallBPriceDetailedAdapter(R.layout.item_small_b_pricedetailed,goodsListBeans);
        priceRecyclerview.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle=new Bundle();
                bundle.putSerializable("PriceDetailedBean",goodsListBeans.get(position));
                RxActivityTool.skipActivity(mContext,SmallBPriceDetailsActivity.class,bundle);
            }
        });
    }

    private void refreshListener() {
        smartrefreshlayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                isClear = true;
                //网络请求
                getJiesuan();
                refreshLayout.finishRefresh();
            }
        });

        smartrefreshlayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                isClear = false;
                page++;
                //网络请求
                getJiesuan();
                refreshLayout.finishLoadMore();
            }
        });
    }


    //查询商店订单数量
    private void getJiesuan() {
        if ("".equals(UserBean.getbToken(this)) || TextUtils.isEmpty(UserBean.getbToken(this))) {
            RxToast.error("请先登录");
        } else {
            OkGo.<String>post(Config.s + "llc/balanceController/queryBalanceByUser")
                    .tag(this)
                    .params("token", UserBean.getbToken(this))
                    .params("page", page)
                    .params("num", 8)
                    .execute(new StringCallback() {
                        @Override
                        public void onSuccess(Response<String> response) {
                            Gson gson = new Gson();
                            QueryBalanceByUserBean bean = gson.fromJson(response.body(), QueryBalanceByUserBean.class);
                            if (bean.isIsSuccess()) {
                                tvPrice.setText(bean.getData().getCount()+"");
                                if (isClear){
                                    goodsListBeans.clear();
                                }
                                goodsListBeans.addAll(bean.getData().getRecord());
                                adapter.setNewData(goodsListBeans);
                            }else {
                                RxToast.error(bean.getOutputMessage());
                            }
                        }

                        @Override
                        public void onError(Response<String> response) {
                            super.onError(response);
                            RxToast.error("服务器连接错误");
                        }
                    });
        }
    }

}
